package com.biel.exrate.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.biel.exrate.entity.exchangeRateEntity;
import com.biel.exrate.mapper.exchangeRateMapper;
//import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Properties;
import com.biel.exrate.entity.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/exRate")
public class ExrateController {
    private static final Logger log = LoggerFactory.getLogger(ExrateController.class);
    @Resource
    exchangeRateMapper exrMapper;
    @GetMapping
    public List<exchangeRateEntity> getExrate() {
        return exrMapper.findAll();
    }

    @PostMapping
    public String addExrate(@RequestBody exchangeRateEntity er){
        //把前端传过来的数据转化为实体类的对象插入到数据库中
        exrMapper.save(er);
        return "S";
    }

    @PostMapping("/batch")
    public Result<exchangeRateEntity> upsertExrateBatch(@RequestBody List<exchangeRateEntity> list){
        try {
            log.info("---------------------------------------------------receive start");
            String json = JSON.toJSONString(list);
            log.info(json);
            exrMapper.insertOrUpdateBatch(list);
            log.info("---------------------------------------------------receive success");
        }
        catch (Exception ex){
            log.info("---------------------------------------------------receive failed");
            log.info(ex.getMessage());
            return ResultResponse.failure(ResultCode.BAD_REQUEST);
        }
        log.info("---------------------------------------------------receive end");
        return ResultResponse.success(list);
    }
/*
    @PutMapping
    public String updateExrate(@RequestBody exrate exr){
        exrMapper.updateByKey(exr);
        return "success";
    }

    @DeleteMapping("/{id}")  //一一对相应的关系
    public String deleteUser(@PathVariable("id") Long id){
        //注解是循序json回传带有id
        exrMapper.deleteById(id);
        return "success";
    }
    @GetMapping("/{id}")  //把返回的结果 返回出来 包装成一个user对象
    public User findById(@PathVariable("id") Long id){
        //注解是循序json回传带有id
        return exrMapper.findById(id);
    }

    @GetMapping("/page")
    public Page<exrate> findByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        Integer offset = (pageNum - 1) * pageSize;
        List<exrate> userData = exrMapper.findByPage(offset, pageSize);
        Page<exrate> page = new Page<>();
        page.setData(userData);
        Integer total = userMapper.countUser();
        page.setTotal(total);
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        return page;
    }
     */
}
